<?php
class WorkAction extends CommonAction {
    
    public function index() {
    	import('ORG.Util.Page');
    	
    	$filter_list = array("type","time","budget","local");
    	
    	$_query = array("endtime"=>array("gt",time()));
    	$_url = U('/work/index')."?";
    	
    	foreach($filter_list as $vo) {
    		$_v = "_".$vo;
    		$$_v = intval($this->_get($vo));
    		if ($$_v) {
    			$_url .= $vo."=".$$_v."&";
    		}
    	}
    	$_name = $this->_get("name");
    	if ($_name) {
    		$_query["name"] = array("like","%".$_name."%");
    		$_url .= "name=".$_name."&";
    	}
    	$_sort = $this->_get("sort");
    	$_sortArr = array(
    		"dateup"=>"pubtime asc",
    		"datedown"=>"pubtime desc",
    		"timeup"=>"endtime asc",
    		"timedown"=>"endtime desc",
    		"budup"=>"budget asc",
    		"budeup"=>"budget desc",
    		"comup"=>"bidcount asc",
    		"comdown"=>"bidcount desc",
    	);
    	$_order = $_sortArr[$_sort];
    	
    	
    	$this->assign("_url",$_url);
    	$this->assign("sort",$_sort);
    	
    	if (!$order) {
    		$order = "id desc";
    	}
    	
    	if ($_type) {
    		$_query["type"] = $_type;
    	}
		if ($_time) {
			$_query["endtime"] = array("between",time().",".(time()+$_time*60*60*24));
		}
		if ($_budget>0) {
			$_barr = array(1=>"0,1000",2=>"1000,3000",3=>"3000,5000",4=>"5000,8000",5=>"8000,10000",6=>"10000,15000",7=>"15000,25000",8=>"25000,40000",9=>"40000,60000",10=>"60000,100000");
			if ($_budget>10) {
				$_query["budget"] = array("gt","100000");
			} else {
				$_query["budget"] = array("between",$_barr[$_budget]);
			}
		}
    	if ($_local && $this->_user) {
    		$_query["vso_user.country"] = $this->_user["country"];
    		$count = M("project")->where($_query)->join("__USER__ on __USER__.id = __PROJECT__.userid")->count();
    	} else {
    		$count = M("project")->where($_query)->count();
    	}
    	
    	/*if ($filter["catelog"]) {
    		$query["vid"] = $filter["catelog"];
    		$count = M("project")->field("`vso_project`.*")->join("__PROJECTMAP__ on __PROJECT__.id = __PROJECTMAP__.pid")->group("`vso_project`.id")->where($query)->count();
    		$Page = new Page($count,15);
    		$data  = M("project")->field("`vso_project`.*")->join("__PROJECTMAP__ on __PROJECT__.id = __PROJECTMAP__.pid")->group("`vso_project`.id")->where($query)->limit($Page->firstRow.','.$Page->listRows)->select();
    	} else {
    	*/
    	
    	
    	$Page = new Page($count,15);
    	$data = M("project")->field('`vso_project`.*,`vso_user`.country')->where($_query)
    				->join("__USER__ on __USER__.id = __PROJECT__.userid")
    				->limit($Page->firstRow.','.$Page->listRows)
    				->order($_order)->select();
    	//echo M("project")->getLastSql();
    	$this->assign("_list",$data);
    	$this->assign("_page",$Page->show());
    	$this->display("work");
    }
    
    public function view() {
    	
    	$wid = intval($this->_get("wid"));
    	if ($this->_user) {
	    	if ($this->_post("action")=="bid") {
	    		import('ORG.Net.UploadFile');
		    	$upload = new UploadFile();// 实例化上传类
				$upload->maxSize  = 1024000 ;// 设置附件上传大小
				$upload->allowExts  = array('zip','rar','doc','7z','pdf','jpg','png');// 设置附件上传类型
				$upload->savePath =  './data/attach/';// 设置附件上传目录
				if(!$upload->upload()) {
					$this->error(L($upload->getErrorMsg()));
				}else{
					$info = $upload->getUploadFileInfo();
					$data = array(
		    			"pid" =>$wid,
		    			"uid" =>$this->_user["id"],
		    			"content" => $this->_post("content"),
		    			"attach" => $info[0]['savename'],
		    			"price" => intval($this->_post("money")),
		    			"status" => 1,
		    			"biddate"=> time(),
		    		);
		    		
		    		M("probid")->data($data)->add();
		    		M("project")->data(array("bidcount"=>countBider($wid)))->where(array("pid"=>$wid))->save();
		    		$this->success("FINISH");
				}	
	    		exit();
	    	} else if ($this->_post("action") == "message"){
	    		$data = array(
	    			"pid" =>$wid,
	    			"uid" =>$this->_user["id"],
	    			"type" => intval($this->_post("type")),
	    			"content" => $this->_post("message"),
	    			"msgdate"=> time(),
	    		);
	    		M("proguestbook")->data($data)->add();
	    		$this->success("FINISH");
	    		exit();
	    	} 
    	}
    		
    		
    	$_data  = M("project")->where(array("id"=>$wid))->find();
    	$_type  = M("projectmap")->join("__CGTYPE__ on __PROJECTMAP__.vid = __CGTYPE__.id")->where(array("pid"=>$wid,"type"=>2))->select();
    	$_skill = M("projectmap")->join("__CGSKILL__ on __PROJECTMAP__.vid = __CGSKILL__.id")->where(array("pid"=>$wid,"type"=>1))->select();
		$_guest = M("proguestbook")->where(array("pid"=>$wid))->join('__USER__ on __USER__.id = __PROGUESTBOOK__.uid')->order("vso_proguestbook.id desc")->limit("0,3")->select();
		//echo M("proguestbook")->getLastSql();
		//var_dump($_guest);
		$_bider = M("probid")->where(array("pid"=>$wid))->join('__USER__ on __USER__.id = __PROBID__.uid')->select();
		$this->assign("work",$_data);
		$this->assign("type",$_type);
		$this->assign("skill",$_skill);
		$this->assign("message",$_guest);
		$this->assign("bider",$_bider);
		$this->display("work-single");
    }
    
    public function message() {
    	$wid = intval($this->_get("wid"));
    	$_data = M("proguestbook")->where(array("pid"=>$wid))->join('__USER__ on __USER__.id = __PROGUESTBOOK__.uid')->order("vso_proguestbook.id desc")->select();
    	$this->assign("message",$_data);
    	$this->assign("wid",$wid);
    	$this->display("work-single-message");
    }
    
    public function bids() {
    	$wid = intval($this->_get("wid"));
    	$_data = M("probid")->where(array("pid"=>$wid))->select();
    	$this->assign("wid",$wid);
    	$this->display("work-single-bid");
    }
    
	public function fav() {
    	$action = $this->_get("action");
    	if ($this->_user) {
    		$wid = intval($this->_get("wid"));
    		if ($action == "cancel") {
    			M("userfav")->where(array("userid"=>$this->_user["id"],"favproid"=>$wid))->delete();
    		} else {
	    		M("userfav")->data(array("userid"=>$this->_user["id"],"favproid"=>$wid))->add();
	    	}
	    	echo json_encode(array("error"=>0,"data"=>favStatus($this->_user,$wid)));
    	} else {
    		echo json_encode(array("error"=>1,"msg"=>L('请先登录')));
    	}
    }
    
    
    protected function _initialize(){
		parent::_initialize();
		$this->assign("nav_menu","work");
	}
}